package com.uniledger.exchange.wallet;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;

import com.uniledger.exchange.HomeActivity;
import com.uniledger.exchange.R;
import com.uniledger.exchange.UniApp;
import com.uniledger.exchange.base.activity.BaseActivity;
import com.uniledger.exchange.wallet.fragment.BackupFragment;
import com.uniledger.exchange.wallet.fragment.OnMnemonicListener;
import com.uniledger.exchange.wallet.fragment.VerifyFragment;

import butterknife.BindString;
import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.internal.operators.completable.CompletableFromAction;
import io.reactivex.schedulers.Schedulers;

/**
 * 备份助记词
 */
public class MnemonicActivity extends BaseActivity implements OnMnemonicListener {

    public static void start(Activity activity, WalletKey walletKey) {
        Intent intent = new Intent(activity, MnemonicActivity.class);
        intent.putExtra("WalletKey", walletKey);
        activity.startActivity(intent);
    }

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, MnemonicActivity.class);
        activity.startActivity(intent);
    }


    @BindView(R.id.btn_confirm)
    Button backup;

    @BindString(R.string.mnemonic_bar_title)
    String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mnemonic);
    }

    @Override
    public void getParams() {
        WalletKey walletKey = getIntent().getParcelableExtra("WalletKey");
        if (walletKey != null) {
            replaceFragment(BackupFragment.newInstance(walletKey, ""), "backup");
        } else {
            UniApp.getDb().keyDao().loadKey(123)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<WalletKey>() {
                        @Override
                        public void accept(WalletKey walletKey) throws Exception {
                            replaceFragment(BackupFragment.newInstance(walletKey, ""), "backup");
                        }
                    });

        }
    }

    @Override
    public void initBar() {
        super.initBar();
        toolbar.setTitle(title);
    }

    private void replaceFragment(Fragment fragment, String name) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.container, fragment);
        transaction.addToBackStack(name);
        // Commit the transaction
        transaction.commit();
    }

    @Override
    public Button getButton() {
        return backup;
    }

    @Override
    public void backup(WalletKey walletKey) {

        replaceFragment(VerifyFragment.newInstance(walletKey, ""), "verify");
    }

    @Override
    public void verify(WalletKey walletKey) {
        if (walletKey.isBackup) {
            //TODO 登录成功
            new CompletableFromAction(new Action() {
                @Override
                public void run() throws Exception {
                    UniApp.getDb().adminDao().update(123, 3);
                    UniApp.getDb().keyDao().update(123, walletKey.id, true);
                }
            }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action() {
                        @Override
                        public void run() throws Exception {
                            HomeActivity.start(MnemonicActivity.this);
                            finish();
                        }
                    });
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_mnemonic, menu);
        return true;
    }


    @Override
    public void onBackPressed() {
        FragmentManager manager = getSupportFragmentManager();
        int count = manager.getBackStackEntryCount();
        if (count == 1) {
            HomeActivity.start(MnemonicActivity.this);
            finish();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        } else if (item.getItemId() == R.id.manger_mnemonic_skip) {
            onBackPressed();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }
}
